using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace StarForce
{
    /// <summary>
    /// 红点系统实现
    /// </summary>
    public partial class RedDotComponent : GameFrameworkComponent
    {
        /// <summary>
        /// 所有的红点ID
        /// </summary>
        private List<int> m_RedId;
                
        /// <summary>
        /// 用于存储红点和对应的显示
        /// </summary>
        private Dictionary<RedDotType,List<GameObject>>  m_RedDots;

        protected override void Awake()
        {
            base.Awake();
            
            m_RedDots = new Dictionary<RedDotType, List<GameObject>>();
            
            m_RedId = new List<int>();
            
            //todo 监听红点变化事件 删除或者增加红点
        }

        /// <summary>
        /// 初始化红点id
        /// </summary>
        /// <param name="ids"></param>
        public void InitRedId(List<int> ids)
        {
            //todo 自定义存储红点数据方法 
            for (int i = 0; i < ids.Count; i++)
            {
                m_RedId.Add(ids[i]);   
            }
        }

        /// <summary>
        /// 注册红点（一个红点只监听一个红点Id）
        /// </summary>
        /// <param name="redDotType"></param>
        /// <param name="gameObject"></param>
        public void RegisterRedDot(RedDotType redDotType,GameObject gameObject)
        {
            if (m_RedDots.ContainsKey(redDotType))
            {
                m_RedDots[redDotType].Add(gameObject);
            }
            else
            {
                List<GameObject> redGameObjects = new List<GameObject>();
                
                redGameObjects.Add(gameObject);
                
                m_RedDots.Add(redDotType,redGameObjects);
            }
            
            gameObject.SetActive(HasRedDotId((int)redDotType));
        }

        /// <summary>
        /// 注册红点（一个红点对应多个红点Id）
        /// </summary>
        /// <param name="redDotTypes"></param>
        /// <param name="gameObject"></param>
        public void RegisterRedDot(List<RedDotType> redDotTypes, GameObject gameObject)
        {
            for (int i = 0; i < redDotTypes.Count; i++)
            {
                RegisterRedDot(redDotTypes[i], gameObject);
            }
        }

        /// <summary>
        /// 取消注册
        /// </summary>
        /// <param name="redDotType"></param>
        /// <param name="gameObject"></param>
        public void UnRegisterRedDot(RedDotType redDotType,GameObject gameObject)
        {
            if (m_RedDots.ContainsKey(redDotType))
            {
                for (int i = 0; i < m_RedDots[redDotType].Count; i++)
                {
                    if (m_RedDots[redDotType].Contains(gameObject))
                    {
                        m_RedDots[redDotType][i].SetActive(false);
                        
                        m_RedDots[redDotType].Remove(gameObject);
                    }
                }

            }
        }

        /// <summary>
        /// 取消注册
        /// </summary>
        /// <param name="redDotTypes"></param>
        /// <param name="gameObject"></param>
        public void UnRegisterRedDot(List<RedDotType> redDotTypes, GameObject gameObject)
        {
            for (int i = 0; i < redDotTypes.Count; i++)
            {
                UnRegisterRedDot(redDotTypes[i], gameObject);
            }   
        }

        /// <summary>
        /// 检查是否存在红点对象
        /// </summary>
        /// <param name="redDotType"></param>
        public bool CheckRedDot(RedDotType redDotType)
        {
            if (m_RedDots.ContainsKey(redDotType))
            {
                for (int i = 0; i < m_RedDots[redDotType].Count; i++)
                {
                    m_RedDots[redDotType][i].SetActive(HasRedDotId((int)redDotType));
                }

                return true;
            }
            return false;
        }
        
        /// <summary>
        /// 检查是否存在红点
        /// </summary>
        /// <param name="redDotTypes"></param>
        /// <param name="objs"></param>
        /// <returns></returns>
        public bool CheckRedDot(List<RedDotType> redDotTypes, object[] objs = null)
        {
            for (int i = 0; i < redDotTypes.Count; i++)
            {
                //只要有一个需要点亮,就显示;
                if (CheckRedDot(redDotTypes[i]))
                {
                    return true;
                }
            }
            return false;
        }


        /// <summary>
        /// 是否存在红点Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private bool HasRedDotId(int id)
        {
            return m_RedId.Contains(id);
        }
        
        /// <summary>
        /// 是否存在红点Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private bool HasRedDotId(List<int> ids)
        {
            for (int i = 0; i < ids.Count; i++)
            {
                if (HasRedDotId(ids[i]))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 添加一个红点Id
        /// </summary>
        /// <param name="id"></param>
        public void AddRedId(int id)
        {
            if (!m_RedId.Contains(id))
            {
                m_RedId.Add(id);
                CheckRedDot((RedDotType) id);
            }
        }

        /// <summary>
        /// 删掉一个红点Id
        /// </summary>
        /// <param name="id"></param>
        public void RemoveRedId(int id)
        {
            if (m_RedId.Contains(id))
            {
                m_RedId.Remove(id);
                CheckRedDot((RedDotType) id);
            }
        }

    }
}